﻿using MechAddin.Common;
using SolidWorks.Interop.sldworks;
using System.Collections.Generic;

namespace MechAddin.Command.移动零件
{
    internal class 移动零件Model : SWBase
    {
        public void 移动零件(double 移动距离, bool 忽略第一个零件)
        {
            if (移动距离 == 0)
            {
                return;
            }
            if (SWDoc2 == null) { return; }
            Face2 face = SelMgr.GetSelectedFace(1);
            if (face == null) { SendMsg("第一个面选择平面"); return; }
            List<Component2> comps = SelMgr.GetSelectedComponents();
            if (comps.Count == 0) { return; }
            MathVector v0 = MathUtility.CreateVector(face.Normal);
            MathTransform TF = comps[0].Transform;
            MathVector v1 = v0.MultiplyTransform(TF);
            int i1 = 0;
            if (忽略第一个零件)
            {
                i1 = 1;
            }
            List<Component2> moveComps = new List<Component2>();
            for (int i = i1; i < comps.Count; i++)
            {
                Component2 comp = comps[i];
                moveComps.Add(comp);
                SWDoc.DeleteMate(comp);
                if (comp.GetParent() is Component2 parent)
                {
                    if (parent.GetModelDoc2() is AssemblyDoc asm)
                    {
                        Component2 comp2 = asm.GetComponentByID(comp.GetID());
                        MathVector v2 = v1.MultiplyTransform(parent.Transform2.Inverse());
                        double[] N1 = v2.ArrayData;
                        double x = N1[0] * 移动距离 * 1e-3;
                        double y = N1[1] * 移动距离 * 1e-3;
                        double z = N1[2] * 移动距离 * 1e-3;
                        double[] xf = comp2.GetXform();
                        xf[9] += x;
                        xf[10] += y;
                        xf[11] += z;
                        comp2.SetXform(xf);
                    }
                }
                else //顶层组件
                {
                    double[] N1 = v1.ArrayData;
                    double x = N1[0] * 移动距离 * 1e-3;
                    double y = N1[1] * 移动距离 * 1e-3;
                    double z = N1[2] * 移动距离 * 1e-3;
                    double[] xf = comp.GetXform();
                    xf[9] += x;
                    xf[10] += y;
                    xf[11] += z;
                    comp.SetXform(xf);
                }
            }
            SWDoc2.FixComponents(moveComps.ToArray());
            SWDoc.EditRebuild3();
        }
        public void 复制移动零件(List<double> 移动距离, bool 忽略第一个零件)
        {
            if (SWDoc2 == null) { return; }
            Face2 face = SelMgr.GetSelectedFace(1);
            if (face == null) { SendMsg("第一个面选择平面"); return; }
            List<Component2> comps = SelMgr.GetSelectedComponents();
            if (comps.Count == 0) { return; }
            MathVector v0 = MathUtility.CreateVector(face.Normal);
            MathTransform TF = comps[0].Transform;
            MathVector v1 = v0.MultiplyTransform(TF);
            int i1 = 0;
            if (忽略第一个零件)
            {
                i1 = 1;
            }
            List<Component2> comps2 = new List<Component2>();
            for (int i = i1; i < comps.Count; i++)
            {
                List<string> paths = new List<string>();
                List<double> trans = new List<double>();
                List<string> strings = new List<string>();
                Component2 comp = comps[i];
                if (comp.GetParent() is Component2 parent)
                {
                    if (parent.GetModelDoc2() is AssemblyDoc asm)
                    {
                        Component2 comp2 = asm.GetComponentByID(comp.GetID());
                        foreach (var dis in 移动距离)
                        {
                            MathVector v2 = v1.MultiplyTransform(parent.Transform2.Inverse());
                            double[] N1 = v2.ArrayData;
                            double x = N1[0] * dis * 1e-3;
                            double y = N1[1] * dis * 1e-3;
                            double z = N1[2] * dis * 1e-3;
                            double[] xf = comp2.GetXform();
                            xf[9] += x;
                            xf[10] += y;
                            xf[11] += z;
                            MathTransform tf = MathUtility.CreateTransform(xf);
                            paths.Add(comp.GetPathName());
                            trans.AddRange(tf.ArrayData);
                            strings.Add(string.Empty);
                        }
                        object[] comps1 = asm.AddComponents3(paths.ToArray(), trans.ToArray(), strings.ToArray());
                        foreach (var item in comps1)
                        {
                            if (item is Component2 component)
                            {
                                comps2.Add(component);
                            }

                        }
                        asm.FixComponents(comps2.ToArray());
                        asm.EditRebuild();
                    }
                }
                else //顶层组件
                {
                    foreach (var dis in 移动距离)
                    {
                        double[] N1 = v1.ArrayData;
                        double x = N1[0] * dis * 1e-3;
                        double y = N1[1] * dis * 1e-3;
                        double z = N1[2] * dis * 1e-3;
                        double[] xf = comp.GetXform();
                        xf[9] += x;
                        xf[10] += y;
                        xf[11] += z;
                        MathTransform tf = MathUtility.CreateTransform(xf);
                        paths.Add(comp.GetPathName());
                        trans.AddRange(tf.ArrayData);
                        strings.Add(string.Empty);
                    }
                    object[] comps1 = SWDoc2.AddComponents3(paths.ToArray(), trans.ToArray(), strings.ToArray());
                    foreach (var item in comps1)
                    {
                        if (item is Component2 component)
                        {
                            comps2.Add(component);
                        }

                    }

                }
            }
            SWDoc2.FixComponents(comps2.ToArray());
            SWDoc2.EditRebuild();
        }
    }
}
